package com.ruoyi.web.controller.my;

import java.util.*;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.constant.my.ReplierConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.domain.MyTheme;
import com.ruoyi.system.service.IMyThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.MyReplier;
import com.ruoyi.system.service.IMyReplierService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 回复人Controller
 *
 * @author ruoyi
 * @date 2023-11-16
 */
@RestController
@RequestMapping("/system/replier")
public class MyReplierController extends BaseController
{
    @Autowired
    private IMyReplierService myReplierService;
    @Autowired
    private IMyThemeService myThemeService;

    /**
     * 回复人下拉框
     */
    @GetMapping("/list1")
    public AjaxResult getRepliers() {
        List<MyReplier> list = myReplierService.list();

        ArrayList<Map> maps = new ArrayList<>();
        for (MyReplier myReplier : list) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", myReplier.getId());
            map.put("nickname", myReplier.getNickname());
            maps.add(map);
        }
        return success(maps);
    }

    /**
     * 查询回复人列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MyReplier myReplier)
    {
        startPage();
        List<MyReplier> list = myReplierService.list();
        System.err.println(list);
        return getDataTable(list);
    }

    /**
     * 获取回复人详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(myReplierService.getById(id));
    }

    /**
     * 新增回复人
     */
    @Log(title = "回复人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyReplier myReplier)
    {
        myReplier.setPassword(Md5Utils.hash(myReplier.getPassword()));
        return toAjax(myReplierService.save(myReplier));
    }

    /**
     * 修改回复人
     */
    @Log(title = "回复人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyReplier myReplier)
    {
        return toAjax(myReplierService.updateById(myReplier));
    }

    /**
     * 删除回复人
     */
    @Log(title = "回复人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        LambdaQueryWrapper<MyTheme> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(MyTheme::getRid,id);
        MyTheme myTheme = myThemeService.getOne(wrapper);
        if(myTheme!=null){
            throw new ServiceException(ReplierConstants.DELETE_ERR);
        }
        return success(myReplierService.removeById(id));
    }
}
